﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace Helpers
{
    class Tools
    {
        static string pathLog = @"C:\Log\";
        static object syncObj = new object();        
        public static void Logger(String mess, String name, String functionName)
        {
            lock (syncObj)
            {
                try
                {
                    Directory.CreateDirectory(pathLog + DateTime.Now.ToString("yyyy-MM-dd"));
                    StreamWriter sw = new StreamWriter(pathLog + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + DateTime.Now.ToString("MM-dd-HH") + name + ".txt", true);

                    sw.WriteLine(DateTime.Now.ToString("hh-mm-ss") + " : [" + functionName + "] :: " + mess);

                    sw.Flush();
                    sw.Close();
                }
                catch
                { }


            }
        }
        public static string Decrypt(string key, string data)
        {
            byte[] keydata = Encoding.ASCII.GetBytes(key);

            string md5String = BitConverter.ToString(new
                                                         MD5CryptoServiceProvider().ComputeHash(keydata)).Replace("-",
                                                                                                                  "").
                Replace(" ", "+").ToLower();

            byte[] tripleDesKey = Encoding.ASCII.GetBytes(md5String.Substring(0, 24));

            TripleDES tripdes = TripleDES.Create();

            tripdes.Mode = CipherMode.ECB;

            tripdes.Key = tripleDesKey;

            byte[] cryptByte = Convert.FromBase64String(data);

            var ms = new MemoryStream(cryptByte, 0, cryptByte.Length);

            ICryptoTransform cryptoTransform = tripdes.CreateDecryptor();

            var decStream = new CryptoStream(ms, cryptoTransform,
                                             CryptoStreamMode.Read);

            var read = new StreamReader(decStream);

            return (read.ReadToEnd());
        }
        
    }
}
